package com.atguigu.daijia.customer.controller;

import com.atguigu.daijia.common.login.GuiguLogin;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.service.OrderService;
import com.atguigu.daijia.model.form.customer.ExpectOrderForm;
import com.atguigu.daijia.model.form.customer.SubmitOrderForm;
import com.atguigu.daijia.model.form.map.CalculateDrivingLineForm;
import com.atguigu.daijia.model.form.payment.CreateWxPaymentForm;
import com.atguigu.daijia.model.vo.base.PageVo;
import com.atguigu.daijia.model.vo.customer.ExpectOrderVo;
import com.atguigu.daijia.model.vo.driver.DriverInfoVo;
import com.atguigu.daijia.model.vo.map.DrivingLineVo;
import com.atguigu.daijia.model.vo.map.OrderLocationVo;
import com.atguigu.daijia.model.vo.map.OrderServiceLastLocationVo;
import com.atguigu.daijia.model.vo.order.CurrentOrderInfoVo;
import com.atguigu.daijia.model.vo.order.OrderInfoVo;
import com.atguigu.daijia.model.vo.payment.WxPrepayVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "订单API接口管理")
@RestController
@RequestMapping("/order")
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 查询乘客执行中订单基本信息
     * @return
     */
    @Operation(summary = "查询乘客执行中订单基本信息")
    @GuiguLogin
    @GetMapping("/searchCustomerCurrentOrder")
    public Result<CurrentOrderInfoVo> searchCustomerCurrentOrder() {
        Long customerId = AuthContextHolder.getUserId();
        CurrentOrderInfoVo currentOrderInfoVo = orderService.searchCustomerCurrentOrder(customerId);
        return Result.ok(currentOrderInfoVo);
    }

    /**
     * 预估订单数据
     * @param expectOrderForm
     * @return
     */
    @Operation(summary = "预估订单数据")
    @GuiguLogin
    @PostMapping("/expectOrder")
    public Result<ExpectOrderVo> expectOrder(@RequestBody ExpectOrderForm expectOrderForm) {
        ExpectOrderVo expectOrderVo = orderService.expectOrder(expectOrderForm);
        return Result.ok(expectOrderVo);
    }

    /**
     * 乘客下单
     * @param submitOrderForm
     * @return
     */
    @Operation(summary = "乘客下单")
    @GuiguLogin
    @PostMapping("/submitOrder")
    public Result<Long> submitOrder(@RequestBody SubmitOrderForm submitOrderForm) {
        submitOrderForm.setCustomerId(AuthContextHolder.getUserId());
        Long orderId = orderService.submitOrder(submitOrderForm);
        return Result.ok(orderId);
    }

    /**
     * 查询订单状态
     * @param orderId
     * @return
     */
    @Operation(summary = "查询订单状态")
    @GuiguLogin
    @GetMapping("/getOrderStatus/{orderId}")
    public Result<Integer> getOrderStatus(@PathVariable("orderId") Long orderId) {
        Integer status = orderService.getOrderStatus(orderId);
        return Result.ok(status);
    }

    /**
     * 根据订单 ID 查询当前乘客的订单信息
     * @param orderId
     * @return
     */
    @Operation(summary = "根据订单 ID 查询当前乘客的订单信息")
    @GuiguLogin
    @GetMapping("/getOrderInfo/{orderId}")
    public Result<OrderInfoVo> getOrderInfo(@PathVariable("orderId") Long orderId) {
        Long customerId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = orderService.getOrderInfo(orderId, customerId);
        return Result.ok(orderInfoVo);
    }

    /**
     * 根据订单 ID 查询司机基本信息
     * @param orderId
     * @return
     */
    @Operation(summary = "根据订单 ID 查询司机基本信息")
    @GuiguLogin
    @GetMapping("/getDriverInfo/{orderId}")
    public Result<DriverInfoVo> getDriverInfo(@PathVariable("orderId") Long orderId) {
        Long customerId = AuthContextHolder.getUserId();
        DriverInfoVo driverInfoVo = orderService.getDriverInfo(orderId, customerId);
        return Result.ok(driverInfoVo);
    }

    /**
     * 从缓存中获取接单司机前往代驾订单起始点过程中的位置信息
     * @param orderId
     * @return
     */
    @Operation(summary = "从缓存中获取接单司机前往代驾订单起始点过程中的位置信息")
    @GuiguLogin
    @GetMapping("/getCacheOrderLocation/{orderId}")
    public Result<OrderLocationVo> getDriverLocationFromCache(@PathVariable("orderId") Long orderId) {
        OrderLocationVo orderLocationVo = orderService.getDriverLocationFromCache(orderId);
        return Result.ok(orderLocationVo);
    }

    /**
     * 计算接单司机前往代驾订单起始点的最佳路线
     * @param calculateDrivingLineForm
     * @return
     */
    @Operation(summary = "计算接单司机前往代驾订单起始点的最佳路线")
    @GuiguLogin
    @PostMapping("/calculateDrivingLine")
    public Result<DrivingLineVo> calculateDriverRouteToOrderStartPoint(@RequestBody CalculateDrivingLineForm calculateDrivingLineForm) {
        DrivingLineVo drivingLineVo = orderService.calculateDriverRouteToOrderStartPoint(calculateDrivingLineForm);
        return Result.ok(drivingLineVo);
    }

    /**
     * 开始代驾服务后，定时获取订单服务期间司机的最新位置信息
     * @param orderId
     * @return
     */
    @Operation(summary = "开始代驾服务后，定时获取订单服务期间最新位置信息")
    @GuiguLogin
    @GetMapping("/getOrderServiceLastLocation/{orderId}")
    public Result<OrderServiceLastLocationVo> getOrderServiceLastLocation(@PathVariable("orderId") Long orderId) {
        OrderServiceLastLocationVo orderServiceLastLocationVo = orderService.getOrderServiceLastLocation(orderId);
        return Result.ok(orderServiceLastLocationVo);
    }

    /**
     * 分页查询乘客订单列表
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页查询乘客订单列表")
    @GuiguLogin
    @GetMapping("/findCustomerOrderPage/{page}/{limit}")
    public Result<PageVo> findCustomerOrderPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Long customerId = AuthContextHolder.getUserId();
        PageVo pageVo = orderService.findCustomerOrderPage(customerId, page, limit);
        return Result.ok(pageVo);
    }

    /**
     * 调用微信 JSAPI 下单接口，获取预支付交易会话标识
     * @param createWxPaymentForm
     * @return
     */
    @Operation(summary = "调用微信 JSAPI 下单接口，获取预支付交易会话标识")
    @GuiguLogin
    @PostMapping("/createWxPayment")
    public Result<WxPrepayVo> createWxPayment(@RequestBody CreateWxPaymentForm createWxPaymentForm) {
        Long customerId = AuthContextHolder.getUserId();
        createWxPaymentForm.setCustomerId(customerId);
        WxPrepayVo wxPrepayVo = orderService.createWxPayment(createWxPaymentForm);
        return Result.ok(wxPrepayVo);
    }

    /**
     * 查询订单支付状态
     * @param orderNo
     * @return
     */
    @Operation(summary ="查询订单支付状态")
    @GuiguLogin
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable("orderNo") String orderNo) {
        Boolean result = orderService.queryPayStatus(orderNo);
        return Result.ok(result);
    }
}

